<?php
class Thread extends MY_Controller {
	
	var $thread;
	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->model('User_model', 'user');
		$this->load->model('Board_model', 'board');
		$this->load->model('Thread_model', 'thread');
		$this->load->model('Message_model', 'message');
	}
	
	//public function _remap($threadID)
	//{
		//$this->thread = $this->thread->get_thread(intval($threadID));
		//if( $this->thread == NULL )
		//{
			//QQQXXX change to error page with redirect option
			//redirect(base_url());
			//return;
		//}
		//$this->index();
	//}
	
	public function index()
	{
		redirect(base_url());
	}
	
	public function view()
	{
		$threadID = $this->uri->segment(3);
		if( $threadID == FALSE )
		{
			redirect(base_url());
			return;
		}
		$this->load->library('typography');
		
		$this->thread = $this->thread->get_thread(intval($threadID));
		
		$this->data['messages'] = $this->message->get_thread_messages($this->thread['ThreadID']);
		$board = $this->board->get_board($this->thread['Board']);
		$this->data['board'] = $board;
		$this->data['thread'] = $this->thread;
	
		$this->data['main_content_view'] = $this->load->view('thread_view', $this->data, true);
		$this->load->view('default', $this->data);
	}
	
	public function post()
	{
		$threadID = $this->uri->segment(3);
		if( $threadID == FALSE )
		{
			redirect(base_url());
			return;
		}
		$this->thread = $this->thread->get_thread(intval($threadID));
		
		$this->data['thread'] = $this->thread;
		$this->data['main_content_view'] = $this->load->view('post_message', $this->data, true);
		$this->load->view('default', $this->data);
		
		$this->form_validation->set_rules('Password', 'Password', 'required');
	}
	
	public function Execute_Post()
	{
		$threadID = intval($this->uri->segment(3));
		if( $threadID == FALSE )
		{
			redirect(base_url());
			return;
		}
		
		$body = $this->input->post('Body');
		if( $body == FALSE )
		{
			redirect(base_url());
			return;
		}
		
		$user = $this->data['user'];
		$fields = array('Thread'=>$threadID, 'User'=>$user['UserID'], 'Body'=>$body);
		$this->message->add_message($fields);
	
		redirect('/Thread/view/'.$threadID);
	}
}
?>